package com.hl.ruoyicsmshop.service.impl;

import java.util.List;
import java.util.Map;
import java.math.BigDecimal;
import java.math.RoundingMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hl.ruoyicsmshop.mapper.OrderStatisticMapper;
import com.hl.ruoyicsmshop.domain.vo.OrderStatisticVO;
import com.hl.ruoyicsmshop.domain.vo.TransactionOverviewVO;
import com.hl.ruoyicsmshop.domain.vo.TransactionTrendVO;
import com.hl.ruoyicsmshop.domain.vo.OrderRefundDetailVO;
import com.hl.ruoyicsmshop.service.IOrderStatisticService;

/**
 * 订单统计Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@Service
public class OrderStatisticServiceImpl implements IOrderStatisticService 
{
    @Autowired
    private OrderStatisticMapper orderStatisticMapper;

    /**
     * 查询热门商品订单数量统计
     * 
     * @param params 查询参数
     * @return 热门商品订单数量统计列表
     */
    @Override
    public List<OrderStatisticVO> selectPopularGoodsByQuantity(Map<String, Object> params)
    {
        return orderStatisticMapper.selectPopularGoodsByQuantity(params);
    }

    /**
     * 查询热门商品订单金额统计
     * 
     * @param params 查询参数
     * @return 热门商品订单金额统计列表
     */
    @Override
    public List<OrderStatisticVO> selectPopularGoodsByAmount(Map<String, Object> params)
    {
        return orderStatisticMapper.selectPopularGoodsByAmount(params);
    }

    /**
     * 查询交易概况统计
     * 
     * @param params 查询参数
     * @return 交易概况统计数据
     */
    @Override
    public TransactionOverviewVO selectTransactionOverview(Map<String, Object> params)
    {
        TransactionOverviewVO overview = orderStatisticMapper.selectTransactionOverview(params);
        
        if (overview != null) {
            // 计算转化率
            if (overview.getVisitorUV() != null && overview.getVisitorUV() > 0) {
                // 下单转化率 = 下单笔数 / 访客数 × 100%
                if (overview.getOrderCount() != null) {
                    BigDecimal orderRate = new BigDecimal(overview.getOrderCount())
                        .divide(new BigDecimal(overview.getVisitorUV()), 4, RoundingMode.HALF_UP)
                        .multiply(new BigDecimal("100"));
                    overview.setOrderConversionRate(orderRate);
                }
                
                // 全店转化率 = 付款笔数 / 访客数 × 100%
                if (overview.getPaymentCount() != null) {
                    BigDecimal overallRate = new BigDecimal(overview.getPaymentCount())
                        .divide(new BigDecimal(overview.getVisitorUV()), 4, RoundingMode.HALF_UP)
                        .multiply(new BigDecimal("100"));
                    overview.setOverallConversionRate(overallRate);
                }
            }
            
            // 付款转化率 = 付款笔数 / 下单笔数 × 100%
            if (overview.getOrderCount() != null && overview.getOrderCount() > 0 
                && overview.getPaymentCount() != null) {
                BigDecimal paymentRate = new BigDecimal(overview.getPaymentCount())
                    .divide(new BigDecimal(overview.getOrderCount()), 4, RoundingMode.HALF_UP)
                    .multiply(new BigDecimal("100"));
                overview.setPaymentConversionRate(paymentRate);
            }
        }
        
        return overview;
    }

    /**
     * 查询交易趋势统计
     * 
     * @param params 查询参数
     * @return 交易趋势统计列表
     */
    @Override
    public List<TransactionTrendVO> selectTransactionTrend(Map<String, Object> params)
    {
        return orderStatisticMapper.selectTransactionTrend(params);
    }

    /**
     * 查询订单详情列表
     * 
     * @param params 查询参数
     * @return 订单详情列表
     */
    @Override
    public List<OrderRefundDetailVO> selectOrderDetailList(Map<String, Object> params)
    {
        return orderStatisticMapper.selectOrderDetailList(params);
    }

    /**
     * 查询退单详情列表
     * 
     * @param params 查询参数
     * @return 退单详情列表
     */
    @Override
    public List<OrderRefundDetailVO> selectRefundDetailList(Map<String, Object> params)
    {
        return orderStatisticMapper.selectRefundDetailList(params);
    }
} 